package com.atguigu.controller;


import com.atguigu.entity.BaseSaleProperty;
import com.atguigu.entity.ProductSpu;
import com.atguigu.result.RetVal;
import com.atguigu.service.BaseSalePropertyService;
import com.atguigu.service.ProductSpuService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 商品图片表 前端控制器
 * </p>
 *
 * @author fumeng
 * @since 2021-10-30
 */
@RestController
@RequestMapping("/product")
public class SpuController {

    @Autowired
    ProductSpuService productSpuService;
    @Autowired
    BaseSalePropertyService baseSalePropertyService;


    //根据categoryId获取商品属性列表
    //http://127.0.0.1/product/queryProductSpuByPage/1/10/1
    @GetMapping("queryProductSpuByPage/{pageNum}/{pageSize}/{category3Id}")
    public RetVal queryProductSpuByPage(@PathVariable Long pageNum,@PathVariable Long pageSize,@PathVariable Long category3Id){
        Page<ProductSpu> spuPage = new Page<>(pageNum,pageSize);

        QueryWrapper<ProductSpu> wrapper = new QueryWrapper<>();
        wrapper
                .eq("category3_id",category3Id)
                .orderByDesc("id");
        productSpuService.page(spuPage, wrapper);

        return RetVal.ok(spuPage);
    }

    //http://127.0.0.1/product/queryAllSaleProperty
    //查询模子销售属性名称
    @GetMapping("queryAllSaleProperty")
    public RetVal queryProductSpuByPage(){
        List<BaseSaleProperty> list = baseSalePropertyService.list(null);
        return RetVal.ok(list);
    }

    //http://127.0.0.1/product/saveProductSpu
    //保存spu添加信息
    @PostMapping("saveProductSpu")
    public RetVal saveProductSpu(@RequestBody ProductSpu productSpu){
        productSpuService.saveProductSpu(productSpu);
        return RetVal.ok();
    }
}

